﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Majoopic.Utility.Image
{
    /// <summary>
    /// Type of decoder for the header of a picture
    /// </summary>
    public enum DecoderType
    {
        /// <summary>
        /// Use the WPF Imaging library
        /// </summary>
        WPFMetadataReader,

        /// <summary>
        /// Use a custom library to read each block header
        /// </summary>
        BlockReader
    }

    /// <summary>
    /// Class to retieve all information about an image
    /// </summary>
    public class ImageDecoder
    {
        #region Private Members

        private DecoderType decoderType = DecoderType.WPFMetadataReader;

        #endregion

        #region Cosntructor

        /// <summary>
        /// Constructor with the type of decoder
        /// </summary>
        public ImageDecoder(DecoderType decoderType)
        {
            this.decoderType = decoderType;
        }

        /// <summary>
        /// Default constructor
        /// </summary>
        public ImageDecoder()
        {

        }

        #endregion

        #region Public Methods
        /// <summary>
        /// Get the header information of the picture
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public ImageHeaderInformation GetImageHeaderInformation(string fileName)
        {
            ImageHeaderInformation imageHeaderInformation = new ImageHeaderInformation();
            BaseImageReader headerReader = this.GetHeaderReader(this.decoderType, fileName);
            using(headerReader)
            {
                headerReader.FillImageHeaderInformation(imageHeaderInformation);
            }
                        
            return imageHeaderInformation;
        }        

        public ImageHeaderInformation GetImageHeaderInformation(string fileName, Stream imageStream)
        {
            return null;
        }
        #endregion

        #region Private Methods
        private BaseImageReader GetHeaderReader(DecoderType decoderType, string fileName)
        {
            BaseImageReader headerReader = null;
            switch(decoderType)
            {
                case DecoderType.BlockReader:
                    headerReader = new BlockImageReader(fileName);
                    break;
                case DecoderType.WPFMetadataReader:
                default:
                    headerReader = new WPFImagingReader(fileName);
                    break;
            }
            return headerReader;
        }
        #endregion
    }
}
